package renshi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import renshi.db.service.UserDao;
import renshi.utils.HttpServletHelper;

/**
 * Servlet implementation class loginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    UserDao ud = new UserDao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	//@SuppressWarnings("null")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json;charset=UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 

		PrintWriter out = response.getWriter();
		JSONArray array = new JSONArray();
		HttpSession session = request.getSession(); 
		
		String method = request.getParameter("method");	
		String username = request.getParameter("username");
			
		if(method==null){
			array.add("Error!");
		}else if(method.equals("login")){
			String password = request.getParameter("password");
			if(ud.login(username, password)){			
				session.setAttribute("loginname", username);			
				array.add("success");
				
				//加入身份判断
				String PRole = ud.loginRole(username);
				session.setAttribute("position", PRole);
				array.add(PRole);
				session.setMaxInactiveInterval(300);
			}else{
				array.add("error");
			}
		}else if(method.equals("islogin")){
			array.add(HttpServletHelper.getResponse(this.isLogin(request, response)));
			array.add(session.getAttribute("loginname"));
			array.add(session.getAttribute("position"));
		}else if(method.equals("logout")){
			this.logout(request, response);
			array.add("success");
		}else if(method.equals("changePasswd")){
			String newPasswd = request.getParameter("newpasswd");
			username=(String)session.getAttribute("loginname");
			array.add(HttpServletHelper.getResponse(ud.changePasswd(username, newPasswd)));
		}
		
		out.print(array);
		out.flush();
		out.close();
		
	}
	
	private boolean isLogin(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
        //String username = request.getParameter("username");
        String str = (String)request.getSession().getAttribute("loginname"); 
        if(str==null||str.equals(""))
        	return false;
        else{
        	return true;
        }
	}
	
	private void logout(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.getSession().removeAttribute("loginname");
		request.getSession().removeAttribute("position");
	}

}
